<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use DB;
class GoodsController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */

    //获取分类并且格式化
    public static function getcates(){
        // 跳转类别和添加分隔符
        //跳转类别顺序连贯方法 Raw 原始表达式，反之sql语句注入
        $cates = DB::table("cates")->select(DB::raw("*,concat(path,',',id)as paths"))->orderBy("paths")->get();
        // 添加等级符号
        foreach($cates as $key => $value){
            $path = $value->path;

            // 转换为数组
            $arr = explode(",",$path);

            $len = count($arr)-1;

            $cates[$key]->name = str_repeat("--|",$len).$value->name;
        }
        return $cates;
    }
    public function index()
    {
        //
        $goods=DB::table('pro_goods')->get();
       return view("Admin.Goods.index",['good'=>$goods]);
    }
 
    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
        $cates=self::getcates();
        return view("Admin.Goods.add",['cate'=>$cates]);
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
       $data=$request->all();
        $data = $request->except('_token');
       // 判断文件是否接受到了
       if($request->hasFile('pic')){
        // 给图片起个名字并且翻到表单数据内
        $data['pic']=uniqid().'.'.$request->file('pic')->getClientOriginalExtension();
        // 添加时间
        $data['up_at']=date("Y-m-d");
        // 链接数据库，吧商品信息储存

        if(DB::table('pro_goods')->insert($data)){
            // 图片重命名后储存起来
        $request->file('pic')->move("./upload/".date("Y-m-d"),$data['pic']);
            return redirect("/admingoods")->with("success","添加成功");
        }else{
            return back()->with("error","添加失败");
        }
       }
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {   
        // 根据id查询出要修改的原数据
        $goods =DB::table("pro_goods")->where("id","=",$id)->get();
        // 转换为一维数组
        $goods=$goods[0];
        // 分类
        $cates=self::getcates();
        return view("Admin.Goods.edit",['cate'=>$cates,'goods'=>$goods]);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {

        
        // 去除两条无关数据
        $data=$request->except("_token","_method");

        // 获取旧图片名
        $opic = DB::table("pro_goods")->where("id","=",$id)->value("pic");
        // 或去旧图片所在文件夹
        $oup_at = DB::table("pro_goods")->where("id","=",$id)->value("up_at")."/";
        // 如果有图片，就储存，并添加到数组，等录入数据库
        if($request->hasFile('pic')){
            $data['pic']=uniqid().'.'.$request->file('pic')->getClientOriginalExtension();
            $request->file('pic')->move("./upload/".date("Y-m-d"),$data['pic']);
        }
        // 修改数据库数据
        if(DB::table("pro_goods")->where("id","=",$id)->update($data)){
            // 如果数据库写入成功，就删除旧图片
            unlink(public_path()."/upload/".$oup_at.$opic);
            return redirect("/admingoods")->with("success","修改成功");
        }else{
            return back()->with("error","修改失败");
        
       }

       //如果修改图片，将图片存入文件，并删除原来文件，下次完成

    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
        // 获取日期文件夹
        $data = DB::table("pro_goods")->where('id','=',$id)->first();
        $datedir =$data->up_at. "/";
        $picname = $data->pic;
        // var_dump(public_path());
       
        if(DB::table("pro_goods")->where('id','=',$id)->delete())
        {
            // 删除文件
            unlink(public_path()."/upload/".$datedir.$picname);
            // 删除完成返回提示；
            return redirect("/admingoods")->with("success","删除成功");
        }
    }



}
